{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# T81-558: Applications of Deep Neural Networks\n",
    "**WSL2 Python Tensorflow Setup**\n",
    "* Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), McKelvey School of Engineering, [Washington University in St. Louis](https://engineering.wustl.edu/Programs/Pages/default.aspx)\n",
    "* For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-558/)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Software Installation\n",
    "This class is technically oriented.  A successful student needs to be able to compile and execute Python code that makes use of TensorFlow for deep learning. There are two options for you to accomplish this:\n",
    "\n",
    "* Install Python, TensorFlow and some IDE (Jupyter, TensorFlow, and others)\n",
    "* Use Google CoLab in the cloud\n",
    "\n",
    "This installation guide is targeted primarily for Windows. Some parts of this document also apply to Linux installs.\n",
    "\n",
    "## Installing for Mac M1/M2\n",
    "\n",
    "If you have a Mac M1 or M2, you can find special installation instructions [here](https://github.com/jeffheaton/t81_558_deep_learning/tree/master/install). Choose the latest date of the Mac install instructions. If you have an Intel Mac, I suggest Google CoLab.\n",
    "\n",
    "## NVIDIA Driver\n",
    "\n",
    "Whether you are installing for Windows or Linux, you must install the latest NVIDIA driver. If you do not have an NVIDIA GPU, then it will be difficult to make use of your GPU for machine learning. I suggest Google CoLab, or CPU-only in this case. You can find the latest NVIDIA drivers here:\n",
    "\n",
    "* [Official NVIDIA Drivers](https://www.nvidia.com/download/index.aspx)\n",
    "\n",
    "## Installing for WSL2 (Windows Only)\n",
    "\n",
    "If you are installing onto Windows either with GPU or without GPU, you should install WSL 2, the Windows subsystem for Linux. I also suggest Windows 11, as it solved some of the issues around the GPU driver. You can skip this section, if you are installing to Linux.\n",
    "\n",
    "Open a PowerShell prompt and issue this command to install WSL2.\n",
    "\n",
    "```\n",
    "wsl --install\n",
    "```\n",
    "\n",
    "You will be directed to reboot your computer. Upon reboot WSL2 will prompt you for a user ID and password. Make sure you remember these, you will need them later.\n",
    "\n",
    "## Installing Python and TensorFlow\n",
    "\n",
    "These instructions apply primarily Windows, Linux is similar; however, for Linux you might want to use a specific guide. Note that I am generally following the instructions [here](https://www.tensorflow.org/install/pip#windows-wsl2); however, customizing it somewhat for the course.\n",
    "\n",
    "The first step is to install Anaconda Python 3.9 into WSL. Anaconda directly supports Windows, Mac, and Linux.  Miniconda is the minimal set of features from the extensive Anaconda Python distribution.  Download Miniconda from the following URL:\n",
    "\n",
    "```\n",
    "curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o Miniconda3-latest-Linux-x86_64.sh\n",
    "bash Miniconda3-latest-Linux-x86_64.sh\n",
    "```\n",
    "\n",
    "Allow it to update your shell. You should exit WSL and reenter it from a PowerShell window.\n",
    "\n",
    "```\n",
    "exit\n",
    "wsl\n",
    "```\n",
    "\n",
    "Next install Jupyter into the base environment.\n",
    "\n",
    "```\n",
    "conda install -y jupyter\n",
    "```\n",
    "\n",
    "We will actually launch Jupyter later.\n",
    "\n",
    "We will now install TensorFlow, Keras, and other tools needed for this course. I created an install YAML file called [tools.yml](https://github.com/jeffheaton/t81_558_deep_learning/blob/master/tools.yml) to perform this installation. You should download this YML file with wget, and then use the YML file to create an environment named **tensorflow**.\n",
    "\n",
    "```\n",
    "wget https://raw.githubusercontent.com/jeffheaton/t81_558_deep_learning/master/tools.yml\n",
    "conda env create -f tools.yml -n tensorflow\n",
    "```\n",
    "\n",
    "To enter this environment, you must use the following command: \n",
    "\n",
    "```\n",
    "conda activate tensorflow\n",
    "```\n",
    "\n",
    "For now, lets add Jupyter support to your new environment.\n",
    "\n",
    "```\n",
    "conda install -c conda-forge nb_conda\n",
    "python -m ipykernel install --user --name tensorflow --display-name \"Python 3.9 (tensorflow)\"\n",
    "```\n",
    "\n",
    "## Setup Library Paths\n",
    "\n",
    "The following command adds CUDA and CUDNN to your system path for WSL2.\n",
    "\n",
    "```\n",
    "mkdir -p $CONDA_PREFIX/etc/conda/activate.d\n",
    "echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/' > $CONDA_PREFIX/etc/conda/activate.d/env_vars.sh\n",
    "```\n",
    "\n",
    "## Testing your Environment\n",
    "\n",
    "The examples in this course has been tested on all of these versions.  The next two sections describe how to install TensorFlow for either a CPU or GPU. To use GPU, you must have a [compatible NVIDIA GPU](https://developer.nvidia.com/cuda-gpus).\n",
    "\n",
    "Before you test this installation it is a good idea to reboot your machine.\n",
    "\n",
    "You can now start Jupyter notebook.  Use the following command.\n",
    "\n",
    "```\n",
    "wsl\n",
    "conda activate tensorflow\n",
    "jupyter notebook\n",
    "```\n",
    "\n",
    "You can now run the following code to check that you have the versions expected."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tensor Flow Version: 2.1.0\n",
      "Keras Version: 2.2.4-tf\n",
      "\n",
      "Python 3.7.7 (default, May  6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)]\n",
      "Pandas 1.0.5\n",
      "Scikit-Learn 0.23.1\n",
      "GPU is available\n"
     ]
    }
   ],
   "source": [
    "# What version of Python do you have?\n",
    "import sys\n",
    "\n",
    "import tensorflow.keras\n",
    "import pandas as pd\n",
    "import sklearn as sk\n",
    "import tensorflow as tf\n",
    "\n",
    "print(f\"Tensor Flow Version: {tf.__version__}\")\n",
    "print(f\"Keras Version: {tensorflow.keras.__version__}\")\n",
    "print()\n",
    "print(f\"Python {sys.version}\")\n",
    "print(f\"Pandas {pd.__version__}\")\n",
    "print(f\"Scikit-Learn {sk.__version__}\")\n",
    "gpu = len(tf.config.list_physical_devices('GPU'))>0\n",
    "print(\"GPU is\", \"available\" if gpu else \"NOT AVAILABLE\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
